/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.DataSet;

import com.nc.kyiv.yellow.sociallibrary.dao.BookDAO;
import com.nc.kyiv.yellow.sociallibrary.POJO.Book;
import com.nc.kyiv.yellow.sociallibrary.POJO.BookAuthor;
import com.nc.kyiv.yellow.sociallibrary.POJO.BookRating;
import com.nc.kyiv.yellow.sociallibrary.helper.EscapeCharacters;
import com.nc.kyiv.yellow.sociallibrary.helper.MapBuilder;
import com.nc.kyiv.yellow.sociallibrary.similarUse.CommonClassesInterface;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Valerie Avramenko
 */
public class GetBook implements CommonClassesInterface {

    private String isbn;

    public GetBook(String isbn) {
        this.isbn = isbn;
    }
    private String[] mapKeys = {"isbn", "dummy1", "dummy2", "dummy3"};
    private MapBuilder helper = new MapBuilder();
    private String result = null;

    @Override
    public String execute(String parameters) throws Exception {
        EscapeCharacters codeDescription = new EscapeCharacters();
        Map postParameters = helper.stringToMap(parameters, mapKeys, helper.getRequestSplitter());

        if (postParameters != null) {

            BookDAO bdao = new BookDAO();
            Book book = bdao.getByISBN(postParameters.get(mapKeys[0]).toString());

            StringBuilder globalLibStr = new StringBuilder();
            if (book != null) {
                globalLibStr.append("{");
                globalLibStr.append("\"image\":\"");
                globalLibStr.append(book.getImageURL());
                globalLibStr.append("\",\"avgRating\":\"");
                globalLibStr.append(book.getAvgRating());
                globalLibStr.append("\",\"sumRating\":\"");
                globalLibStr.append(book.getSumRating());
                globalLibStr.append("\",\"currentUserScore\":\"");
                globalLibStr.append(book.getCurrentUserScore());
                globalLibStr.append("\",\"title\":\"");
                globalLibStr.append(book.getTitle());
                globalLibStr.append("\",\"description\":\"");
                globalLibStr.append(codeDescription.EscapeCharactersForJSON(book.getDescription()));
                globalLibStr.append("\",");
                List<BookRating> rating = book.getRating();
                Map ratingMarks = new HashMap<Integer, Integer>();
                for (int i = 1; i < 6; i++) {
                    ratingMarks.put(i, 0);
                }
                for (BookRating br : rating) {
                    ratingMarks.put(br.getRatingScore(), br.getSumVotes());
                }
                for (int i = 1; i < 6; i++) {
                    globalLibStr.append("\"voteFor");
                    globalLibStr.append(i);
                    globalLibStr.append("\":\"");
                    globalLibStr.append(ratingMarks.get(i));
                    globalLibStr.append("\",");
                }

                List<BookAuthor> authors = book.getAuthors();
                globalLibStr.append("\"authors\":\"");
                for (BookAuthor bk : authors) {
                    if (bk.getFirstName() != null) {
                        globalLibStr.append(" ");
                        globalLibStr.append(bk.getFirstName());
                    }
                    if (bk.getMiddleName() != null) {
                        globalLibStr.append(" ");
                        globalLibStr.append(bk.getMiddleName());
                    }
                    if (bk.getLastName() != null) {
                        globalLibStr.append(" ");
                        globalLibStr.append(bk.getLastName());
                    }
                    globalLibStr.append(",");
                }
                globalLibStr.append("\",");

                globalLibStr.append("\"ISBN\":\"");
                globalLibStr.append(book.getISBN());
                globalLibStr.append("\"}");
                

                result = globalLibStr.toString();
                System.out.println("Result: " + result + " and globalLib:" + globalLibStr.toString());
                
            } else {
                return "failed";
            }

            return result;

        } else {
            return "failed";
        }
    }
}
